AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

local Energy_Increment = 2500 --due to the fact that it is huge and it needs interactive fuel
local Coolant_Increment = 100

function ENT:Initialize()
	self.Entity:PhysicsInit( SOLID_VPHYSICS )
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetSolid( SOLID_VPHYSICS )
	
	local phys = self.Entity:GetPhysicsObject()
	self.val1 = 0
	self.gotfuel = 0
	self.overdrive = 0
	self.overdrivefactor = 0 --will  be the same as overdrive but I'm to lazy to modify it
	self.damaged = 0
	self.critical = 0
	
	if not (WireAddon == nil) then self.Inputs = Wire_CreateInputs(self.Entity, { "On", "Overdrive" }) end
	if not (WireAddon == nil) then self.Outputs = Wire_CreateOutputs(self.Entity, { "Out" }) end

	self.Entity:SetColor( 10, 96, 255, 255 )
	self.timer = CurTime() +  1
	if (phys:IsValid()) then
		phys:Wake()
		phys:SetMass(3000)
	end
end

function ENT:Setup()
	self:TriggerInput("On", 0)
end

function ENT:TriggerInput(iname, value)
	if (iname == "On") then
		if (value ~= 0) then
			if ( self.Active == 0 ) then
				self.Active = 1
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		else
			if ( self.Active == 1 ) then
				self.Active = 0
				Wire_TriggerOutput(self.Entity, "Out", self.Active)
			end
		end
	elseif (iname == "Overdrive") then
		if (value > 0) then
			self.overdrive = 1
			self.overdrivefactor = value
		else
			self.overdrive = 0
			self.overdrivefactor = 0
		end
	end
end

function ENT:Use()
end

function ENT:Damage()
	if (self.damaged == 0) then
		self.damaged = 1
	end
end

function ENT:Repair()
	self.Entity:SetColor( 10, 96, 255, 255 )
	self.health = self.max_health
	self.damaged = 0
	self.critical = 0
end

function ENT:Destruct()
		local function Func1(Entity)
			if Entity:IsValid() then
				local Effect = EffectData()
					Effect:SetOrigin(Entity:GetPos() + Vector( math.random(-60, 60), math.random(-60, 60), math.random(-60, 60) ))
					Effect:SetScale(1)
					Effect:SetMagnitude(25)
					util.Effect("Explosion", Effect, true, true)
			end
		end
		local function Func2(Entity)
			if Entity:IsValid() then
				local Effect = EffectData()
					Effect:SetOrigin(Entity:GetPos())
					Effect:SetScale(3)
					Effect:SetMagnitude(100)
					util.Effect("Explosion", Effect, true, true)

					Entity:Remove()
			end
		end
		timer.Simple(1, Func1, self.Entity)
		timer.Simple(1.2, Func1, self.Entity)
		timer.Simple(2, Func1, self.Entity)
		timer.Simple(2, Func2, self.Entity)
		self.Entity:Remove()
end

function ENT:OnRemove()
	Dev_Unlink_All(self.Entity)
end


function ENT:Output()
	return 1
end

function ENT:Extract_Energy()
	local inc = Energy_Increment
	local hck = 0
	if ( self.overdrive > 0 ) then
		inc = math.Round(inc * self.overdrivefactor)
		if self.overdrivefactor > 3 then
			self:Destruct()
		else
			if self.overdrive > 1 then
				DamageLS(self, math.random(0, math.Round(self.overdrivefactor*3)))
			end
		end
		hck = self.overdrive
	else
		hck = 1
	end
	
	if (self.critical == 1) then
		local Smoke = ents.Create("env_smoketrail")
		Smoke:SetKeyValue("opacity", 1)
		Smoke:SetKeyValue("spawnrate", 10)
		Smoke:SetKeyValue("lifetime", 2)
		Smoke:SetKeyValue("startcolor", "180 180 180")
		Smoke:SetKeyValue("endcolor", "255 255 255")
		Smoke:SetKeyValue("minspeed", 15)
		Smoke:SetKeyValue("maxspeed", 30)
		Smoke:SetKeyValue("startsize", (self.Entity:BoundingRadius() / 2))
		Smoke:SetKeyValue("endsize", self.Entity:BoundingRadius())
		Smoke:SetKeyValue("spawnradius", 10)
		Smoke:SetKeyValue("emittime", 300)
		Smoke:SetKeyValue("firesprite", "sprites/firetrail.spr")
		Smoke:SetKeyValue("smokesprite", "sprites/whitepuff.spr")
		Smoke:SetPos(self.Entity:GetPos())
		Smoke:SetParent(self.Entity)
		Smoke:Spawn()
		Smoke:Activate()
		Smoke:Fire("kill","", 1)
	end
	
	if self.gotfuel > 0 then
		if (inc > 0) then
			RD_SupplyResource(self, "energy", inc)
		end
	else
		self:TriggerInput("On", 0)
	end
	
	--coolant check
	if (RD_GetResourceAmount(self, "coolant") <=  math.Round(self.overdrive*Coolant_Increment)) then
		self:TriggerInput("On", 0)
	else
		RD_ConsumeResource(self, "coolant", math.Round(hck*Coolant_Increment))
	end
	self.gotfuel = self.gotfuel-1
	return
end



function ENT:Think()
	if ( self.Active == 0 ) then
		self:SetOverlayText( "Fission Core\n(OFF)\nCoolant: " .. self.val1.."\n Uranium Fuel (g): "..self.gotfuel.."\n")
	else
		if (self.overdrive > 0) then
			self:SetOverlayText( "Fission Core\n(ON)\n(OVERDRIVE)\nCoolant: " .. self.val1.."\n Uranium Fuel (g): "..self.gotfuel.."\n")
		else
			self:SetOverlayText( "Fission Core\n(ON)\nCoolant: " .. self.val1.."\n Uranium Fuel (g): "..self.gotfuel.."\n")
		end
	end
	if ( self.timer > CurTime() ) then return end
	if (self.Active == 1) then
		self:Extract_Energy()
	end
	if (self.damaged == 1) then
		self.critical = 1
	end
	self.val1 = RD_GetResourceAmount(self, "coolant")
	
	local pos = self.Entity:GetPos()+(self.Entity:GetForward()*100)+(self.Entity:GetRight()*-20)+(self.Entity:GetUp()*50)
	local ents = ents.FindInSphere(pos, 70)
	for k,v in pairs(ents) do
		if (v:GetClass() == "sent_subcriticalmass") or (v:GetClass() == "sent_uraniumresource") then
			v:Remove()
			self.gotfuel = self.gotfuel+50
		end
	end
	self.timer = CurTime() +  1
end


function ENT:AcceptInput(name,activator,caller)
	if name == "Use" and caller:IsPlayer() and caller:KeyDownLast(IN_USE) == false then
		if ( self.Active == 0 ) then
			self.Active = 1
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 1) end
		else
			self.Active = 0
			if not (WireAddon == nil) then Wire_TriggerOutput(self.Entity, "Out", 0) end
		end
	end
end


function ENT:PreEntityCopy()
	RD_BuildDupeInfo(self.Entity)
	if (WireAddon == 1) then
		local DupeInfo = Wire_BuildDupeInfo(self.Entity)
		if DupeInfo then
			duplicator.StoreEntityModifier( self.Entity, "WireDupeInfo", DupeInfo )
		end
	end
end

function ENT:PostEntityPaste( Player, Ent, CreatedEntities )
	RD_ApplyDupeInfo(Ent, CreatedEntities)
	if (WireAddon == 1) then
		if (Ent.EntityMods) and (Ent.EntityMods.WireDupeInfo) then
			Wire_ApplyDupeInfo(Player, Ent, Ent.EntityMods.WireDupeInfo, function(id) return CreatedEntities[id] end)
		end
	end
end
